/*
 * 项目名称:    wms-server-jeecg
 * 创建人员:    DongPeng
 * 创建时间:    2023-07-31
 * copyright(c) 2022 无锡星智数服科技有限公司
 */
package org.jeecg.modules.system.rule;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.handler.IFillRuleHandler;
import org.jeecg.config.init.SequenceConfig;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 业务单据编码通用规则
 *
 * @author DongPeng
 * @version 1.0.0
 * @date 2023-07-31
 * @description 用雪花算法保证不同机房不同服务器序列号不重复
 */
@Component
public class BasicCodeRule implements IFillRuleHandler {

    @Override
    public Object execute(JSONObject params, JSONObject formData) {
        //默认取4位不重复的流水序列
        int number = 4;
        String prefix = "";
        if (params != null) {
            Object obj_num = params.get("number");
            if (obj_num != null) number = Integer.parseInt(obj_num.toString());
            Object obj_prefix = params.get("prefix");
            if (obj_prefix != null) prefix = obj_prefix.toString();
        }
        long seq = SequenceConfig.sequence.nextValue();
        String code = String.format("%0" + number + "d", seq);
        if (StrUtil.isNotEmpty(prefix)) {
            code = prefix.trim() + code;
        }
        return code;
    }
}
